<?php

namespace common\models\wechat;

use common\models\business\BusinessUser;
use Yii;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "wechat_fans".
 *
 * @property string $id
 * @property string $wid
 * @property string $openid
 * @property string $nickname
 * @property string $groupid
 * @property string $salt
 * @property integer $follow
 * @property string $followtime
 * @property string $unfollowtime
 * @property string $tag
 * @property string $updatetime
 * @property string $unionid
 */
class WechatFans extends \yii\db\ActiveRecord
{

    public $ttag = 0;   //打上标签还是去掉标签

    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '粉丝',      //用于修改了什么的显示
        'mainfield' => 'nickname',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];




    /**
     * 取消关注
     */
    const STATUS_UNSUBSCRIBED = -1;
    /**
     * 关注状态
     */
    const STATUS_SUBSCRIBED = 0;
    public static $statuses = [
        self::STATUS_SUBSCRIBED => '关注',
        self::STATUS_UNSUBSCRIBED => '取消关注'
    ];



    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'wechat_fans';
    }



    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['wid', 'follow', 'followtime', 'unfollowtime', 'updatetime','tagsid'], 'integer'],
            [['openid', 'nickname'], 'string', 'max' => 50],
            [['avatar'], 'string', 'max' => 255],
            [['unionid'], 'string', 'max' => 64],
            [['remark'],'string','max'=>30],
            [['tagsname'],'string','max'=>20],
            [['openid'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'openid' => 'Openid',
            'avatar' => '头像',
            'nickname' => '昵称',
            'follow' => '是否关注',
            'followtime' => '关注时间',
            'unfollowtime' => '取消关注时间',
            'updatetime' => '更新时间',
            'unionid' => 'Unionid',
            'remark' => '备注',
            'tagsid' => '标签',
            'tagsname' => '标签名称',
        ];
    }



    /**
     * 关联的用户信息
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(BusinessUser::className(), ['fansid' => 'id']);
    }



    /**
     * 关联公众号
     * @return \yii\db\ActiveQuery
     */
    public function getWechat()
    {
        return $this->hasOne(WechatAccounts::className(), ['id' => 'wid']);
    }



    /**
     * 通过唯一的openid查询粉丝
     * @param $open_id
     * @return mixed
     */
    public static function findByOpenId($open_id)
    {
        return self::findOne(['openid' => $open_id]);
    }

    /**
     * 关注
     * @return bool
     */
    public function subscribe()
    {
        return $this->updateAttributes(['follow' => self::STATUS_SUBSCRIBED]) > 0;
    }

    /**
     * 取消关注
     * @return bool
     */
    public function unsubscribe()
    {
        return $this->updateAttributes(['follow' => self::STATUS_UNSUBSCRIBED]) > 0;
    }

    /**
     * 更新用户数据
     * @param bool $force
     * @return bool
     */
    public function updateUser($force = false)
    {
        $user = $this->user;
        if (!$user || $force) {
            $user = new BusinessUser();
            $data = \Yii::$app->wechat->user->get($this->openid);
            if ($data) {
                $user->setAttributes([
                    'id' => $this->id,
                    'fansid' => $this->id,
                    'nickname' => $data->nickname,
                    'gender' => $data->sex==1?'男':'女',
                    'openid' => $data->openid,
                    'address' => $data->country.$data->city,
                    'avatar' => $data->headimgurl,
                    'createtime' => time()
                ]);
                $user->save();
                return $user->save();
            }
            return false;
        }
        return true;
    }
}
